<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /**解法一
         * @param {TreeNode} root
         * @param {number} k
         * @return {number}
         */
        var kthLargest = function (root, k) {
            // 反中序遍历，记录数值到数组，获取第k -1 个
            let setArray = [];
            const dfs = function (node) {
                if (node === null) {
                    return
                }
                dfs(node.right);
                setArray.push(node.val);
                dfs(node.left);
            }
            dfs(root);
            return setArray[k - 1];

        };
        /**解法二
         * @param {TreeNode} root
         * @param {number} k
         * @return {number}
         */
        var kthLargest = function (root, k) {
            // 反中序遍历，记录数值第k个值返回
            let num = 0;
            let result = null;
            const dfs = function (node) {
                if (node === null) {
                    return;
                }
                dfs(node.right);
                num++;
                if (num === k) {
                    result = node.val;
                    return;
                }
                dfs(node.left);
            }
            dfs(root);
            return result;
        };


    </script>
</body>

</html>